Pick a color image you like. Instead of loading an image in grayscale as we have done earlier, we will try reading it as is (without a flag). Expectedly, we obtain a BGR image. We then convert it to grayscale using OpenCV cvtColor function. Look familiar? This is the same function that we use to convert from BGR to RGB. The only difference is the transform specification: cv2.COLOR_BGR2GRAY. We display side-by-side the color and gray images.
imC = cv2.imread('tree.jpg')
im = cv2.cvtColor(imC, cv2.COLOR_BGR2GRAY)
weight = 100 #Specify a constant to add/subtract to an image
brighter = cv2.add(im,weight) #Image addition, an image is uniformly brighter
Next, we apply the same five operations: brighter/darker, higher/lower contrast, and negative to a color image. Unlike grayscale, we now have three channels that we need to handle. The arithmetic is essentially the same as before but we repeat it three times for three channels. As an example, we pick another color image to work on and detail how to brighten it in two steps. The code and the result is shown below.
weight = 100
brighter = np.ones(im.shape,dtype='uint8')
for i in np.arange(0,3):
brighter[:,:,i] = cv2.add(im[:,:,i], weight)